﻿// <copyright file="UserException.cs" company="Ecole Normale Supérieure">
// Released under GPLv2
// </copyright>
// <summary>Contains the user exception base class</summary>

namespace Sqwarea.GameLogic.Exceptions
{
    using System;
    using System.Runtime.Serialization;
    using System.Security.Permissions;
    using Sqwarea.Persistence;

    /// <summary>
    /// User exception base class : all user exception must inherit from this one
    /// </summary>
    [Serializable]
    public class UserException : Exception
    {
        /// <summary>
        /// Initializes a new instance of the UserException class
        /// </summary>
        /// <param name="user">User causing the exception</param>
        /// <param name="message">Exception message</param>
        public UserException(User user, string message) : base(message)
        {
            this.User = user;
        }

        /// <summary>
        /// Initializes a new instance of the UserException class
        /// </summary>
        /// <param name="message">Exception message</param>
        public UserException(string message)
            : base(message)
        {
        }

        /// <summary>
        /// Initializes a new instance of the UserException class
        /// </summary>
        public UserException() : base()
        {
        }

        /// <summary>
        /// Initializes a new instance of the UserException class
        /// </summary>
        /// <param name="message">Exception message</param>
        /// <param name="ex">Parent exception</param>
        public UserException(string message, Exception ex) : base(message, ex)
        {
        }

        /// <summary>
        /// Initializes a new instance of the UserException class
        /// </summary>
        /// <param name="info">Serialization infos</param>
        /// <param name="context">Streaming context</param>
        protected UserException(SerializationInfo info, StreamingContext context) 
            : base(info, context)
        {
        }

        /// <summary>
        /// Gets the user causing the exception
        /// </summary>
        public User User { get; private set; }

        /// <summary>
        /// Gets object data
        /// </summary>
        /// <param name="info">Serialization info</param>
        /// <param name="context">Streaming context</param>
        [SecurityPermission(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.SerializationFormatter)]
        public override void GetObjectData(SerializationInfo info, StreamingContext context)
        {
            base.GetObjectData(info, context);
            info.AddValue("User", this.User);
        }
    }
}
